﻿package model.utils  {
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import flash.events.Event;	
	import flash.display.Sprite;
	import flash.events.ProgressEvent;
	import view.components.utils.UIEvent;

	/**
	 * @author Lobanov Aleksey
	 * 
	 * simple loading XML from target url :) very simple
	 */
	public class DataXmlLoader extends Sprite {
	
		static public var XML_LOADING_SUCCESS : String = "onXmlLoadingComplete";
		private var _bulkLoader:BulkLoader;

		public function DataXmlLoader($link:String) {
			_bulkLoader = new BulkLoader("xml loading");
			
			_bulkLoader.add($link, { id:"xml" } );
			_bulkLoader.addEventListener(BulkLoader.COMPLETE, onCompleteHandler);
			_bulkLoader.addEventListener(BulkLoader.PROGRESS, onProgressHandler);
			_bulkLoader.start();
		}
		
		private function onProgressHandler(evt:ProgressEvent):void {
			//trace("Loaded" , evt.bytesLoaded," of ",  evt.bytesTotal);
		}
		
		private function onCompleteHandler(evt:ProgressEvent):void {		
			dispatchEvent(new UIEvent(XML_LOADING_SUCCESS, _bulkLoader.getXML("xml", true)));
			_bulkLoader.clear();
		}
	}
}